<?php

/*

Desktopd Colors
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

*/


namespace Desktopd\Colors;


class Plot {
    protected $x_min;
    protected $y_min;
    
    protected $zoom;
    
    protected $image;
    
    protected function createImage($width, $height) {
        $image = imagecreatetruecolor($width, $height); // 8-bit sRGB color
        
        imagesavealpha($image, true);
        imagealphablending($image, false);
        $transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
        imagefill($image, 0, 0, $transparent);
        //imageantialias($image, true);
        
        return $image;
    }
    
    public function __construct($minWidth, $minHeight, array $x, array $y) {
        $x_min = min($x);
        $x_max = max($x);
        
        $y_min = min($y);
        $y_max = max($y);
        
        $x_median = $x_max - $x_min / 2;
        $x_range = $x_max - $x_min;
        
        $y_median = $y_max - $y_min / 2;
        $y_range = $y_max - $y_min;
        
        if ($y_range > $x_range) {
            if ($y_range / $x_range > $minHeight / $minWidth) {
                $zoom = $minWidth / $x_range;
                $width = $minWidth;
                $height = $y_range * $zoom;
            } else {
                $zoom = $minHeight / $y_range;
                $height = $minHeight;
                $width = $x_range * $zoom;
            }
        } else {
            if ($x_range / $y_range > $minWidth / $minHeight) {
                $zoom = $minHeight / $y_range;
                $height = $minHeight;
                $width = $x_range * $zoom;
            } else {
                $zoom = $minWidth / $x_range;
                $width = $minWidth;
                $height = $y_range * $zoom;
            }
        }
        
        $this->image = $this->createImage($width, $height);
        $this->zoom = $zoom;
        $this->x_min = $x_min;
        $this->y_min = $y_min;
    }
    
    public function plot($x, $y, sRGB $color) {
        if (!is_resource($this->image)) return false;
        
        $x = ($x - $this->x_min) * $this->zoom;
        $y = imagesy($this->image) - ($y - $this->y_min) * $this->zoom;
        
        if ($x < 0 || $y < 0 || $x > imagesx($this->image) || $y > imagesy($this->image)) var_dump(array($x, $y), array(imagesx($this->image), imagesy($this->image)), "$color");
        
        list($R, $G, $B) = array_map(function($c) {
            return max(0, min(255, $c * 256));
        }, array($color->getR(), $color->getG(), $color->getB()));
        
        $color = imagecolorallocatealpha($this->image, $R, $G, $B, 0);
        return imagefilledellipse($this->image, $x, $y, 16, 16, $color);
    }
    
    public function save($path) {
        $result = imagepng($this->image, $path, 9);
        imagedestroy($this->image);
        return $result;
    }
}


// vim: ts=4 et ai

